
# -*- coding:utf-8 -*-
from flask import request, jsonify, abort
from flask_cors import CORS
from com.zzh.dao_v2.messageDao import MessageAPI
from com.zzh.dao_v2.AuthTokenOperation import authTokenOperation
import time
from . import app

localTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))


CORS(app, supports_credentials=True)




# ------------------ message表API ---------------
@app.route('/queryMessageById', methods=['POST'])   #根据message-type查询
def queryMessageByIdNoToken():
    id = request.json.get('id')
    try:
        queryResult, MessageListJson = MessageAPI().queryMessage(id)
        if queryResult:
            return jsonify({"info": MessageListJson, "totalCount": queryResult,
                            "message": {"code": 1, "info": "消息查询成功！"}})
        else:
            return jsonify({"info": [], "message": {"code": 1, "status": "No data"}})
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})



@app.route('/queryMessageByCondition', methods=['POST'])
def queryMessageByConditionNoToken():


        if not request.json:
            abort(400)

        try:
            page = request.json['page']
            pageIndex = page["pageIndex"]
            pageSize = page["pageSize"]
        except:
            return jsonify({"message": {"code": 0, "status": "请传入 pageIndex和pageSize"}})
        try:
            conditions = request.json['condition']
        except:
            return jsonify({"message": {"code": 0, "status": "未传入指定condition"}})
        try:
            multiSort = request.json['multiSort']
        except:
            return jsonify({"message": "未传入排序规则multiSort"})

        try:
            UrlManageJSON, total_count =  MessageAPI().queryMessageByCondition(pageIndex=pageIndex, pageSize=pageSize,
                                                                                  conditions=conditions,
                                                                                  multiSort=multiSort)

            if total_count:
                return jsonify(
                    {"message": {"code": 1, "status": "querymessage OK"}, "total": total_count, "info": UrlManageJSON})
            else:
                return jsonify({"info": [], "message": {"code": 0, "status": "No data"}})
        except Exception, e:
                return jsonify({"message": {"code": 0, "info": str(e)}})









@app.route('/addMessage', methods=['POST'])
def addMessageNoToken():
    if not request.json:
        abort(400)
    message_info = {
        'messageType': request.json.get('messageType', 0),
        'postId': request.json.get('postId', 0),
        'postName': request.json.get('postName' ),
        'acceptId': request.json.get('acceptId', 0),
        'acceptName': request.json.get('acceptName'),
        'messageIsRead': request.json.get('messageIsRead', 0),
        'messageTitle': request.json.get('messageTitle'),
        'messageContent': request.json.get('messageContent' ),
        'messagePostTime': localTime,
        'messageReadTime': request.json.get('messageReadTime' ),
    }
    try:
        rs = MessageAPI().addMessage(message_info)
        if rs:
            return jsonify({  "message": {"code": 1, "info": "添加成功！"}})
        else:
            return jsonify({"message": {"code": 0, "info": "添加失败！"}})
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})


@app.route('/updateMessage', methods=['POST'])
def updateMessageNoToken():

        if not request.json:
            abort(400)



        try:
            acceptId = request.json.get('acceptId')
            messageId = request.json.get('messageId')
            isLick = request.json.get('isLock', 0)
            messageIsRead = request.json.get('messageIsRead', 0)
        except:
            return jsonify({"message": {"code": 0, "status": "缺少参数"}})
        try:
            updateMessageResult = MessageAPI().updateMessageById(acceptId, messageId, isLick, messageIsRead)
            if updateMessageResult:
                return jsonify({"info": updateMessage,
                                "massage": {"code": 1, "info": "消息修改成功"}})
            else:
                return jsonify({"message": {"code": 0, "info": "消息修改失败"}})
        except Exception, e:
            return jsonify({"message": {"code": 0, "info": str(e)}})




@app.route('/deleteMessages', methods=['POST'])
def deleteMessagesNoToken():
    id = request.json.get('ids')
    try:
        deleteMessageResult = MessageAPI().deleteMessageById(id)
        if deleteMessageResult:
            return jsonify({"message": {"code": 1, "info": "删除消息记录成功！"}})
        else:
            return jsonify({"message": {"code": 0, "info": "删除消息记录失败！"}})
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})




